﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using LanceZhang.DBUtility;

namespace RealTimePriceFetchService.DAL
{
    /// <summary>
    /// 数据访问类RealTimeStockInfoDAL。
    /// </summary>
    public class RealTimeStockInfoDAL
    {
        public RealTimeStockInfoDAL()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RealTimePriceFetchService.Model.RealTimeStockInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RealTimeStockInfo(");
            strSql.Append("StockCode,CurrentPrice,CurrentCount,Buy1Price,Buy1Count,Buy2Price,Buy2Count,Buy3Price,Buy3Count,Buy4Price,Buy4Count,Buy5Price,Buy5Count,Sale1Price,Sale1Count,Sale2Price,Sale2Count,Sale3Price,Sale3Count,Sale4Price,Sale4Count,Sale5Price,Sale5Count,CurrentTime)");
            strSql.Append(" values (");
            strSql.Append("@StockCode,@CurrentPrice,@CurrentCount,@Buy1Price,@Buy1Count,@Buy2Price,@Buy2Count,@Buy3Price,@Buy3Count,@Buy4Price,@Buy4Count,@Buy5Price,@Buy5Count,@Sale1Price,@Sale1Count,@Sale2Price,@Sale2Count,@Sale3Price,@Sale3Count,@Sale4Price,@Sale4Count,@Sale5Price,@Sale5Count,@CurrentTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@StockCode", SqlDbType.Int,4),
					new SqlParameter("@CurrentPrice", SqlDbType.Decimal,9),
					new SqlParameter("@CurrentCount", SqlDbType.Int,4),
					new SqlParameter("@Buy1Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy1Count", SqlDbType.Int,4),
					new SqlParameter("@Buy2Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy2Count", SqlDbType.Int,4),
					new SqlParameter("@Buy3Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy3Count", SqlDbType.Int,4),
					new SqlParameter("@Buy4Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy4Count", SqlDbType.Int,4),
					new SqlParameter("@Buy5Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy5Count", SqlDbType.Int,4),
					new SqlParameter("@Sale1Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale1Count", SqlDbType.Int,4),
					new SqlParameter("@Sale2Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale2Count", SqlDbType.Int,4),
					new SqlParameter("@Sale3Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale3Count", SqlDbType.Int,4),
					new SqlParameter("@Sale4Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale4Count", SqlDbType.Int,4),
					new SqlParameter("@Sale5Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale5Count", SqlDbType.Int,4),
					new SqlParameter("@CurrentTime", SqlDbType.DateTime)};
            parameters[0].Value = model.StockCode;
            parameters[1].Value = model.CurrentPrice;
            parameters[2].Value = model.CurrentCount;
            parameters[3].Value = model.Buy1Price;
            parameters[4].Value = model.Buy1Count;
            parameters[5].Value = model.Buy2Price;
            parameters[6].Value = model.Buy2Count;
            parameters[7].Value = model.Buy3Price;
            parameters[8].Value = model.Buy3Count;
            parameters[9].Value = model.Buy4Price;
            parameters[10].Value = model.Buy4Count;
            parameters[11].Value = model.Buy5Price;
            parameters[12].Value = model.Buy5Count;
            parameters[13].Value = model.Sale1Price;
            parameters[14].Value = model.Sale1Count;
            parameters[15].Value = model.Sale2Price;
            parameters[16].Value = model.Sale2Count;
            parameters[17].Value = model.Sale3Price;
            parameters[18].Value = model.Sale3Count;
            parameters[19].Value = model.Sale4Price;
            parameters[20].Value = model.Sale4Count;
            parameters[21].Value = model.Sale5Price;
            parameters[22].Value = model.Sale5Count;
            parameters[23].Value = model.CurrentTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(RealTimePriceFetchService.Model.RealTimeStockInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RealTimeStockInfo set ");
            strSql.Append("StockCode=@StockCode,");
            strSql.Append("CurrentPrice=@CurrentPrice,");
            strSql.Append("CurrentCount=@CurrentCount,");
            strSql.Append("Buy1Price=@Buy1Price,");
            strSql.Append("Buy1Count=@Buy1Count,");
            strSql.Append("Buy2Price=@Buy2Price,");
            strSql.Append("Buy2Count=@Buy2Count,");
            strSql.Append("Buy3Price=@Buy3Price,");
            strSql.Append("Buy3Count=@Buy3Count,");
            strSql.Append("Buy4Price=@Buy4Price,");
            strSql.Append("Buy4Count=@Buy4Count,");
            strSql.Append("Buy5Price=@Buy5Price,");
            strSql.Append("Buy5Count=@Buy5Count,");
            strSql.Append("Sale1Price=@Sale1Price,");
            strSql.Append("Sale1Count=@Sale1Count,");
            strSql.Append("Sale2Price=@Sale2Price,");
            strSql.Append("Sale2Count=@Sale2Count,");
            strSql.Append("Sale3Price=@Sale3Price,");
            strSql.Append("Sale3Count=@Sale3Count,");
            strSql.Append("Sale4Price=@Sale4Price,");
            strSql.Append("Sale4Count=@Sale4Count,");
            strSql.Append("Sale5Price=@Sale5Price,");
            strSql.Append("Sale5Count=@Sale5Count,");
            strSql.Append("CurrentTime=@CurrentTime");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@StockCode", SqlDbType.Int,4),
					new SqlParameter("@CurrentPrice", SqlDbType.Decimal,9),
					new SqlParameter("@CurrentCount", SqlDbType.Int,4),
					new SqlParameter("@Buy1Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy1Count", SqlDbType.Int,4),
					new SqlParameter("@Buy2Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy2Count", SqlDbType.Int,4),
					new SqlParameter("@Buy3Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy3Count", SqlDbType.Int,4),
					new SqlParameter("@Buy4Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy4Count", SqlDbType.Int,4),
					new SqlParameter("@Buy5Price", SqlDbType.Decimal,9),
					new SqlParameter("@Buy5Count", SqlDbType.Int,4),
					new SqlParameter("@Sale1Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale1Count", SqlDbType.Int,4),
					new SqlParameter("@Sale2Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale2Count", SqlDbType.Int,4),
					new SqlParameter("@Sale3Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale3Count", SqlDbType.Int,4),
					new SqlParameter("@Sale4Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale4Count", SqlDbType.Int,4),
					new SqlParameter("@Sale5Price", SqlDbType.Decimal,9),
					new SqlParameter("@Sale5Count", SqlDbType.Int,4),
					new SqlParameter("@CurrentTime", SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.StockCode;
            parameters[2].Value = model.CurrentPrice;
            parameters[3].Value = model.CurrentCount;
            parameters[4].Value = model.Buy1Price;
            parameters[5].Value = model.Buy1Count;
            parameters[6].Value = model.Buy2Price;
            parameters[7].Value = model.Buy2Count;
            parameters[8].Value = model.Buy3Price;
            parameters[9].Value = model.Buy3Count;
            parameters[10].Value = model.Buy4Price;
            parameters[11].Value = model.Buy4Count;
            parameters[12].Value = model.Buy5Price;
            parameters[13].Value = model.Buy5Count;
            parameters[14].Value = model.Sale1Price;
            parameters[15].Value = model.Sale1Count;
            parameters[16].Value = model.Sale2Price;
            parameters[17].Value = model.Sale2Count;
            parameters[18].Value = model.Sale3Price;
            parameters[19].Value = model.Sale3Count;
            parameters[20].Value = model.Sale4Price;
            parameters[21].Value = model.Sale4Count;
            parameters[22].Value = model.Sale5Price;
            parameters[23].Value = model.Sale5Count;
            parameters[24].Value = model.CurrentTime;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete RealTimeStockInfo ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RealTimePriceFetchService.Model.RealTimeStockInfoModel GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,StockCode,CurrentPrice,CurrentCount,Buy1Price,Buy1Count,Buy2Price,Buy2Count,Buy3Price,Buy3Count,Buy4Price,Buy4Count,Buy5Price,Buy5Count,Sale1Price,Sale1Count,Sale2Price,Sale2Count,Sale3Price,Sale3Count,Sale4Price,Sale4Count,Sale5Price,Sale5Count,CurrentTime from RealTimeStockInfo ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            RealTimePriceFetchService.Model.RealTimeStockInfoModel model = new RealTimePriceFetchService.Model.RealTimeStockInfoModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StockCode"].ToString() != "")
                {
                    model.StockCode = int.Parse(ds.Tables[0].Rows[0]["StockCode"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CurrentPrice"].ToString() != "")
                {
                    model.CurrentPrice = decimal.Parse(ds.Tables[0].Rows[0]["CurrentPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CurrentCount"].ToString() != "")
                {
                    model.CurrentCount = int.Parse(ds.Tables[0].Rows[0]["CurrentCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy1Price"].ToString() != "")
                {
                    model.Buy1Price = decimal.Parse(ds.Tables[0].Rows[0]["Buy1Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy1Count"].ToString() != "")
                {
                    model.Buy1Count = int.Parse(ds.Tables[0].Rows[0]["Buy1Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy2Price"].ToString() != "")
                {
                    model.Buy2Price = decimal.Parse(ds.Tables[0].Rows[0]["Buy2Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy2Count"].ToString() != "")
                {
                    model.Buy2Count = int.Parse(ds.Tables[0].Rows[0]["Buy2Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy3Price"].ToString() != "")
                {
                    model.Buy3Price = decimal.Parse(ds.Tables[0].Rows[0]["Buy3Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy3Count"].ToString() != "")
                {
                    model.Buy3Count = int.Parse(ds.Tables[0].Rows[0]["Buy3Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy4Price"].ToString() != "")
                {
                    model.Buy4Price = decimal.Parse(ds.Tables[0].Rows[0]["Buy4Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy4Count"].ToString() != "")
                {
                    model.Buy4Count = int.Parse(ds.Tables[0].Rows[0]["Buy4Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy5Price"].ToString() != "")
                {
                    model.Buy5Price = decimal.Parse(ds.Tables[0].Rows[0]["Buy5Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Buy5Count"].ToString() != "")
                {
                    model.Buy5Count = int.Parse(ds.Tables[0].Rows[0]["Buy5Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale1Price"].ToString() != "")
                {
                    model.Sale1Price = decimal.Parse(ds.Tables[0].Rows[0]["Sale1Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale1Count"].ToString() != "")
                {
                    model.Sale1Count = int.Parse(ds.Tables[0].Rows[0]["Sale1Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale2Price"].ToString() != "")
                {
                    model.Sale2Price = decimal.Parse(ds.Tables[0].Rows[0]["Sale2Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale2Count"].ToString() != "")
                {
                    model.Sale2Count = int.Parse(ds.Tables[0].Rows[0]["Sale2Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale3Price"].ToString() != "")
                {
                    model.Sale3Price = decimal.Parse(ds.Tables[0].Rows[0]["Sale3Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale3Count"].ToString() != "")
                {
                    model.Sale3Count = int.Parse(ds.Tables[0].Rows[0]["Sale3Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale4Price"].ToString() != "")
                {
                    model.Sale4Price = decimal.Parse(ds.Tables[0].Rows[0]["Sale4Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale4Count"].ToString() != "")
                {
                    model.Sale4Count = int.Parse(ds.Tables[0].Rows[0]["Sale4Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale5Price"].ToString() != "")
                {
                    model.Sale5Price = decimal.Parse(ds.Tables[0].Rows[0]["Sale5Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sale5Count"].ToString() != "")
                {
                    model.Sale5Count = int.Parse(ds.Tables[0].Rows[0]["Sale5Count"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CurrentTime"].ToString() != "")
                {
                    model.CurrentTime = DateTime.Parse(ds.Tables[0].Rows[0]["CurrentTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,StockCode,CurrentPrice,CurrentCount,Buy1Price,Buy1Count,Buy2Price,Buy2Count,Buy3Price,Buy3Count,Buy4Price,Buy4Count,Buy5Price,Buy5Count,Sale1Price,Sale1Count,Sale2Price,Sale2Count,Sale3Price,Sale3Count,Sale4Price,Sale4Count,Sale5Price,Sale5Count,CurrentTime ");
            strSql.Append(" FROM RealTimeStockInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "RealTimeStockInfo";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
